import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;

// Transform the algebraic expression with brackets into RPN form (Reverse
// Polish Notation). Two-argument operators: +, -, *, /, ^ (priority from the
// lowest to the highest), brackets ( ). Operands: only letters: a,b,...,z.
// Assume that there is only one RPN form (no expressions like a*b*c).
//
// Input:
// 3
// (a+(b*c))
// ((a+b)*(z+x))
// ((a+t)*((b+(a+c))^(c+d)))
//
// Output:
// abc*+
// ab+zx+*
// at+bac++cd+^*

public class ONP {

	public static void main(String[] args) {
		// priorities
		Map<Character, Integer> priority = new HashMap<Character, Integer>();
		priority.put('+', 1);
		priority.put('-', 2);
		priority.put('*', 3);
		priority.put('/', 4);
		priority.put('^', 5);
		// priority.put('(', 6);
		// priority.put(')', 6);

		Scanner s = new Scanner(System.in);
		int t = s.nextInt();
		while (t-- > 0) {
			Stack<Character> stack = new Stack<Character>();
			// Stack<Character> operands = new Stack<Character>();
			StringBuilder postfix = new StringBuilder();

			String expr1 = s.next();
			char[] expr = expr1.toCharArray();
			// stack.push('#');
			for (int i = 0; i < expr.length; i++) {
				char c = expr[i];

				if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
					postfix.append(c);
				} else {
					if (c == '+' || c == '-' || c == '*' || c == '/'
							|| c == '^' || c == '(' || c == ')') {
						switch (c) {
						case '(':
							stack.push(c);
							break;
						case '-':
						case '+':
						case '*':
						case '/':
						case '^':
							if (c == '^')
								;
							while (!stack.isEmpty()
									&& stack.peek() != '('
									&& priority.get(c) < priority.get(stack
											.peek())) {
								postfix.append(stack.pop());
							}
							stack.push(c);
							break;
						case ')':
							while (!stack.isEmpty() && stack.peek() != '(') {
								postfix.append(stack.pop());
							}
							stack.pop();
							break;
						}
					}

				}
				// System.out.println();
				// System.out.println("Char= " + c);
				// System.out.println("stack= " + stack);
				// System.out.println("postfix= " + postfix.toString());
			}
			while (!stack.isEmpty()) {

				postfix.append(stack.pop());

			}
			System.out.println(postfix);
		}
		s.close();
	}
}